package com.rong.tools.controller;

import com.rong.tools.entity.dto.ToolDto;
import com.rong.tools.entity.pojo.Tool;
import com.rong.tools.service.ToolService;
import com.rong.tools.entity.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 周厚荣
 * @date 2025/4/20 23:20
 */
@RestController
@RequestMapping("/tool")
public class ToolController extends ABaseController{
    @Autowired
    private ToolService toolService;
    @GetMapping("/getAllTools")
    public ResponseVO getAllTools() {
        List<Tool> allTools = toolService.getAllTools();
        return getSuccessResponseVO(allTools);
    }

    @GetMapping("/getToolById/{id}")
    public ResponseVO getToolById(@PathVariable Integer id) {
        Tool toolById = toolService.getToolById(id);
        return getSuccessResponseVO(toolById);
    }

    @GetMapping("/getToolByCategory/{categoryId}")
    public ResponseVO getToolByCategory(@PathVariable Integer categoryId) {
        List<Tool> toolByCategory = toolService.getToolByCategory(categoryId);
        return getSuccessResponseVO(toolByCategory);
    }

    @PostMapping("/addTool")
    public ResponseVO addTool(@RequestBody Tool tool) {
        toolService.addTool(tool);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/updateTool")
    public ResponseVO updateTool(@RequestBody Tool tool) {
        toolService.updateTool(tool);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/deleteTool/{id}")
    public ResponseVO deleteTool(@PathVariable Integer id) {
        toolService.deleteTool(id);
        return getSuccessResponseVO(null);
    }
    @GetMapping("/getToolWithCategoryName")
    public ResponseVO getToolWithCategoryName() {
        List<ToolDto> toolWithCategoryName = toolService.getToolWithCategoryName();
        return  getSuccessResponseVO(toolWithCategoryName);
    }

    @PostMapping("/updateWithCategoryName")
    public ResponseVO updateWithCategoryName(@RequestBody ToolDto toolDto) {
        toolService.updateWithCategoryName(toolDto);
        return getSuccessResponseVO(null);
    }

    @PostMapping("/addWithCategoryName")
    public ResponseVO addWithCategoryName(@RequestBody ToolDto toolDto) {
        toolService.addWithCategoryName(toolDto);
        return  getSuccessResponseVO(null);
    }

    @GetMapping("/search/{keyword}")
    public ResponseVO search(@PathVariable String keyword) {
        List<Tool> search = toolService.search(keyword);
        return getSuccessResponseVO(search);
    }


}
